from tb_info.models import *


# def get_org_str(userID):
#     org_list = TbUserOrg.objects.filter(user_id=userID)
#     if len(org_list) > 1:
#         result = ""
#         for org in org_list:
#             result += org.org.get_full_org() + "|"
#         return result[:-1]  # 去掉最後面多餘的一個'|'
#     elif len(org_list) == 1:
#         return org_list[0].org.get_full_org()
#     else:
#         return ""


# def get_full_org(self, listType=False):
#     """
#     默认返回字符串类型的Org鏈，中间以'-'符号分隔；
#     listType為True時返回列表類型的Org鏈
#     :param listType: False
#     :return: string full Org
#     """
#     parent_id = self.parent_id
#     if listType:
#         org = [self.org_name]
#     else:
#         org = self.org_name
#     while parent_id:
#         org_obj = TbOrg.objects.filter(pk=parent_id, is_active=True)
#
#         # 假如用parent查询的Org_id不存在，（assert False）抛出AssertionError异常
#         assert org_obj, ("org_id '%s' absent" % parent_id)
#         # if not org_obj:
#
#
#         parent_id = org_obj[0].parent_id
#         if listType:
#             org = org.insert(0, org_obj[0].org_name)
#         else:
#             org = org_obj[0].org_name + '-' + org
#     return org


def get_full_org2(parent_id):
    t = TbOrg.objects.filter(id=int(parent_id),is_active=True)
    a = t.values("parent_id")[0]["parent_id"]
    b = t.values("org_level_id")[0]["org_level_id"]
    # print(a,b,type(b))
    while int(b) == 25:
        c = t.values("id")[0]["id"]
        return c,b
    return get_full_org2(a)


def downtree(lit1,list3,list4):
    list2 = []
    if lit1:
        for orgID in lit1:
            # print(orgID)
            m = TbOrg.objects.filter(parent_id=orgID,is_active=True)
            # print(m,'ccccccccccccc')
            if len(m) >= 1:
                # list3 = [up_id.id for up_id in m]
                for up_id in m:
                    org_id = up_id.id
                    list2.append(org_id)
                    list4.append(org_id)
                # print(list2)
                # return downtree(list2,list3)
            elif not m.exists():
                # print(orgID)
                list3.append(orgID)
        return downtree(list2, list3,list4)
    return list3,list4


def get_org_people(org_id):
    list1 = []
    lit1 = []
    lit2 = []
    lit3 = []
    lit1.append(org_id)
    org_list1, org_list2 = downtree(lit1, lit2, lit3)
    org_list = list(set(org_list2).union(set(org_list1)))
    org_list.append(org_id)
    for orgID in org_list:
        orgName = TbOrg.objects.filter(id=orgID).values("org_name")[0]["org_name"]
        org_num = TbUserOrg.objects.filter(org_id=orgID).count()

        dicts = {"org_id":orgID,"org_name":orgName,"num":org_num}
        list1.append(dicts)
    return list1


def get_full_org3(org_id):
    t = TbOrg.objects.filter(id=int(org_id),is_active=True)
    # print(t)
    a = t.values("parent_id")[0]["parent_id"]
    b = t.values("org_level_id")[0]["org_level_id"]
    # print(a,b,type(b))
    num = t.values("org_people")[0]["org_people"]

    org_all = TbOrg.objects.filter(id=a,is_active=True)
    all_num = org_all.values("org_people_all")[0]["org_people_all"]
    org_all.update(org_people_all=int(all_num)+int(num))
    # print(num,all_num)
    # print(org_id)
    # while int(b) == 25:
    #     c = t.values("id")[0]["id"]
    #     return c,b
    # if not org_id:
    #
    #     return True
    # return get_full_org3(a)


